﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Crystal.CLL;
using Crystal.Common.Model;
using CrystalCommon.BLL;

namespace CrystalCommon.Presenter
{
    public class AddValuePresenter : IAddValuePresenter
    {
        #region Fields
        public IAddValueView view;
        public CrystalEntities context;
        #endregion

        #region Constructor
        public AddValuePresenter(IAddValueView view)
        {
            this.view = view;
            this.view.Add += view_Add;
            this.view.GetTableName += view_GetTableName;
            context = new CrystalEntities(CommonCLL.mstrContextConnectionstring );
        }
        #endregion

        #region Event Handlers
        public void view_Add(string name, EntityType type, ref string id)
        {
            EntityTypeManager manager = new EntityTypeManager();
                string tableName = view_GetTableName(type.SourceId);
            if (tableName == context.LKP_HeadInvestor.EntitySet.Name)
            {
                LKP_HeadInvestor headInvestor = new LKP_HeadInvestor();
                headInvestor.HeadInvestor = name;
                var q = from c in context.LKP_HeadInvestor
                        where c.HeadInvestor  == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddHeadInvestor(context, headInvestor);
                    var maxID = from c in context.LKP_HeadInvestor
                                where c.HeadInvestor  == headInvestor.HeadInvestor
                                select c;
                    id = maxID.ToList().First().HeadInvestorID.ToString();
                }
                else
                    id = q.ToList().First().HeadInvestorID.ToString();

            }

            else if (tableName == context.LKP_InvestorType.EntitySet.Name)
            {
                LKP_InvestorType investorType = new LKP_InvestorType();
                investorType.InvestorType = name;
                var q = from c in context.LKP_InvestorType
                        where c.InvestorType == investorType.InvestorType
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddInvestorType(context, investorType);
                    var maxID = from c in context.LKP_InvestorType
                            where c.InvestorType == investorType.InvestorType
                            select c;
                    id = maxID.ToList().First().InvestorTypeID.ToString();
                }
                else
                    id= q.ToList().First().InvestorTypeID.ToString();
                    
            }
            else if (tableName == context.LKP_Style.EntitySet.Name)
            {
                LKP_Style style = new LKP_Style();
                style.Style = name;
                var q = from c in context.LKP_Style
                        where c.Style  == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddStyle(context, style);
                    var maxID = from c in context.LKP_Style
                                where c.Style  == name
                                select c;
                    id = maxID.ToList().First().StyleID.ToString();
                }
                else
                    id = q.ToList().First().StyleID.ToString();

            }
            else if (tableName == context.LKP_Parent.EntitySet.Name)
            {
                LKP_Parent parent = new LKP_Parent();
                parent.Parent = name;
                //investorType.InvestorType = name;
                var q = from c in context.LKP_Parent
                        where c.Parent  == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddParent(context, parent);
                    var maxID = from c in context.LKP_Parent
                                where c.Parent == name
                                select c;
                    id = maxID.ToList().First().ParentID.ToString() ;
                }
                else
                    id = q.ToList().First().ParentID.ToString();
            }
            else if (tableName == context.LKP_UltimateParent.EntitySet.Name)
            {
                LKP_UltimateParent ultimateParent = new LKP_UltimateParent();
                ultimateParent.UltimateParent = name;
                var q = from c in context.LKP_UltimateParent
                        where c.UltimateParent == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddUltimateParent(context, ultimateParent);
                    var maxID = from c in context.LKP_UltimateParent
                                where c.UltimateParent == name
                                select c;
                    id = maxID.ToList().First().UltimateParentID.ToString() ;
                }
                else
                    id = q.ToList().First().UltimateParentID.ToString();

            }
            else if (tableName == context.LKP_City.EntitySet.Name)
            {
                LKP_City city = new LKP_City();
                city.City = name;
                if (type.Parameter.HasValue)
                {
                    city.CountryID = type.Parameter.Value;
                }
                else
                {
                    return;
                }
                var q = from c in context.LKP_City
                        where c.City == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddCity(context, city);
                    var maxID = from c in context.LKP_City
                                where c.City == name
                                select c;
                    id = maxID.ToList().First().CityID.ToString() ;
                }
                else
                    id = q.ToList().First().CityID.ToString() ;
            }
            else if (tableName == context.LKP_Activist.EntitySet.Name)
            {
                LKP_Activist activist = new LKP_Activist();
                activist.ActivistCategory = name;
                var q = from c in context.LKP_Activist
                        where c.ActivistCategory == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddActivist(context, activist);
                    var maxID = from c in context.LKP_Activist
                                where c.ActivistCategory == name
                                select c;
                    id = maxID.ToList().First().ActivistID.ToString() ;
                }
                else
                    id = q.ToList().First().ActivistID.ToString();
            }

            else if (tableName == context.LKP_Country.EntitySet.Name)
            {
                LKP_Country country = new LKP_Country();
                country.CountryName = name;
                var q = from c in context.LKP_Country
                        where c.CountryName == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddCountry(context, country);
                    var maxID = from c in context.LKP_Country
                                where c.CountryName == name
                                select c;
                    id = maxID.ToList().First().CountryID.ToString() ;
                }
                else
                    id = q.ToList().First().CountryID.ToString();
            }
            else if (tableName == context.LKP_FundType.EntitySet.Name)
            {
                LKP_FundType fundType = new LKP_FundType();
                fundType.FundTypeDescription = name;
                var q = from c in context.LKP_FundType
                        where c.FundTypeDescription == name
                        select c;
                string word = GetFundTypeCode(name,0,3);
                fundType.FundType = word;
                if (q.Count() == 0)
                {
                    manager.AddFundType(context, fundType );
                    var maxID = from c in context.LKP_FundType 
                                where c.FundTypeDescription == name
                                select c;
                    id = maxID.ToList().First().FundTypeID.ToString();
                }
                else
                    id = q.ToList().First().FundTypeID.ToString();
            }
            else if (tableName == context.EDS_Templates.EntitySet.Name)
            {
                EDS_Templates template = new EDS_Templates();
                template.ETemplateName = name;
                if (type.Parameter.Value > 0)
                    template.ESourceID = type.Parameter.Value;
                else
                    template.ESourceID = 1;
                var q = from c in context.EDS_Templates
                        where c.ETemplateName == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddTemplate(context, template);
                    var maxID = from c in context.EDS_Templates
                                where c.ETemplateName == name
                                select c;
                    id = maxID.ToList().First().ETemplateID.ToString();
                }
                else
                    id = q.ToList().First().ETemplateID.ToString();
            }
            else if (tableName == context.EDS_DataSources.EntitySet.Name)
            {
                EDS_DataSources datasources = new EDS_DataSources();
                datasources.ESourceName = name;
                if (type.Parameter.Value > 0)
                {
                    datasources.SourceTypeID = type.Parameter.Value;
                }
                    var q = from c in context.EDS_DataSources
                        where c.ESourceName == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddSourceName(context, datasources);
                    var maxID = from c in context.EDS_DataSources
                                where c.ESourceName == name
                                select c;
                    id = maxID.ToList().First().ESourceID.ToString();
                }
                else
                    id = q.ToList().First().ESourceID.ToString();
            }

            else if (tableName == context.LKP_Listing.EntitySet.Name)
            {
                LKP_Listing listing = new LKP_Listing();
                listing.ListingName = name;
                var q = from c in context.LKP_Listing
                        where c.ListingName == name
                        select c;
                if (q.Count() == 0)
                {
                    manager.AddListing(context, listing);
                    var maxID = from c in context.LKP_Listing
                                where c.ListingName == name
                                select c;
                    id = maxID.ToList().First().ListingID.ToString();
                }
                else
                    id = q.ToList().First().ListingID.ToString();

            }
            //'LKP_HeadInvestor headInvestor = new LKP_HeadInvestor();
            //'headInvestor.
            //type.
            //using (EntityTypeManager clsEntityTypeManager = new EntityTypeManager())
            //{

            //    string enu = enmEntityType.Template.ToString();
            //    if (type.Name == enmEntityType.Template.ToString())
            //    {
            //        EDS_Templates templates = new EDS_Templates();
            //        templates.ETemplateName = name;
            //        templates.ESourceID = type.SourceId;
            //        clsEntityTypeManager.AddEntityTemplateType(context, templates);
            //    }
            //    else if (type.Name == enmEntityType.Source.ToString())
            //    {
            //        EDS_DataSources dataSource = new EDS_DataSources();
            //        dataSource.ESourceName = type.Name;
            //        dataSource.SourceTypeID= type.SourceId;
            //        clsEntityTypeManager.AddEntityDataSourceType(context, dataSource);
            //    }
            //}
        }
        public string GetFundTypeCode(string name,int startIndex,int Lenght)
        {
            string s = name;

            // Split string based on spaces
            string[] words = s.Split(new char[] { ' ' });

            // Use For Each loop over words and display them
            string word = null;
            foreach (string word_loopVariable in words)
            {
                word = word_loopVariable.Substring(startIndex , Lenght).ToUpper();
                break;
            }
            LKP_FundType fundType = new LKP_FundType();
            var q = from c in context.LKP_FundType
                    where c.FundType == word 
                    select c;
            if (q.Count() > 0)
            {
                word =  GetFundTypeCode(name, startIndex +1, 3);
            }
            return word;
        }
        public string view_GetTableName(byte id)
        {
            EntityTypeManager manager = new EntityTypeManager();
            string tableName = manager.GetTableName(context, id);
            return tableName;
        }
        #endregion
    }
}
